/**
Bus Planner
Copyright 2011 Ernesto Angel Celis de la Fuente 
<celisdelafuente@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/

/**
 * StreetView of stops
 * */
function showStreetView(latLng) {
	panOptions = {
		position: latLng,
		pov: {
			pitch: 10,
			zoom: 1
		}
	};
	pano = new google.maps.StreetViewPanorama(
		document.getElementById("map_canvas"),
		panOptions
	);
	pano.setVisible(true);
	map.setStreetView(pano);
}


/**
  Check current position to be within city bounds.
 **/
function checkCityBounds(detectedLocation) {
	if(!cityMapBounds.contains(detectedLocation)) {
		alert("La posición actual esta fuera de los límites de " +
			"esta aplicación. Te voy a enviar a la posición " +
			"default.");
		currentLocation = defaultLocation;
	} else {
		currentLocation = detectedLocation;
	}
	map.setCenter(currentLocation);
}


/**
  Add one stop marker
  */
function addStop(cl) {
	stopMarker = new google.maps.Marker({
		map: map,
		draggable: true,
		animation: google.maps.Animation.DROP,
		position: cl 
		});
	fillFtable(stopMarker.getPosition());
	ftable_edit();
	google.maps.event.addListener(stopMarker, 
		'dragend', 
		function() {fillFtable(stopMarker.getPosition())}
	);
}


/**
  Populate coordinates in the Add Stop Box 
  when draggin the marker 
 */
function fillFtable(cl) {
	$("#posicion").html(cl.toString());	
	$("#posfield").val(cl.toString());
}


/**
  Hide and show the Add Stop box
  **/
function ftable_edit() {
	var options = {};
	if($("#hiddenctrl").html() == "-") {
		$("#tv_ftform").hide("slow");
		$("#tv_hide_infobox").html(
			'<img src="/images/control-double-180.png" ' +
			'alt="Mostrar" />');
		$("#hiddenctrl").html("+");
	} else {
		options = { to: { width: 164 }};
		$("#tv_ftform").show("size", options, "slow");
		$("#tv_hide_infobox").html(
			'<img src="/images/control-double-000.png" ' +
			' alt="Ocultar" />');
		$("#hiddenctrl").html("-");
		$("#tv_ftform").css("width","164px");
	}
}


function voteUp(latLng) {
}


function voteDown() {
}

/**
 * Get user's current location
 * */
function geoLocation() {
	// Try W3C Geolocalization (Preferred)
	if(navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(
			function(position) {
				detectedLocation =
					new google.maps.LatLng(
						position.coords.latitude,
						position.coords.longitude
					);
				checkCityBounds(detectedLocation);
			},
			function() {
				handleNoGeoLocation(browserSupportFlag);
			}
		);
		// TODO I think Gears has been deprecated, check that!
	} else if (google.gears) {
		// Try Google Gears
		browserSupportFlag = true;
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(
			function(position) {
				detectedLocation = 
					new google.maps.LatLng(
						position.latitude,
						position.longitude
					);
				checkCityBounds(detectedLocation);
			},
			function() {
				handleNoGeoLocation(browserSupportFlag);
			}
		);
	} else {
		browserSupportFlag = false;
		handleNoGeoLocation(browserSupportFlag);
	}
}

/**
 * SELECT from Fusiontables
 * */
function ftSelect(layer, fields, condition) {
	var qrystr = 'ROWID,' + fields.toString();
	var query = ftQuery(ftid, qrystr, condition);
	var ftOptions = new google.maps.FusionTablesOptions();
	ftOptions.query = query;
	layer.setOptions(ftOptions);
	layer.setMap(map);
}

function ftQuery(table, qrystr, condition) {
	var query = new google.maps.FusionTablesQuery();
	query.from = table;
	query.select = qrystr;
	if(condition != "" || condition != null) {
		query.where = condition;
	}
	return query;
}
